package visiopuce.ui.providers;

import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;

import visiopuce.objets.Alteration;
import visiopuce.objets.Analyse;
import visiopuce.objets.Bande;
import visiopuce.objets.Resultat;
import visiopuce.objets.VerifBiomol;
import visiopuce.objets.VerifFish;
import visiopuce.service.AnalyseServiceImpl;
import visiopuce.service.ReferentielServiceImpl;
import visiopuce.service.ResultatServiceImpl;

public class CNVTableLabelProvider extends ColumnLabelProvider implements IColorProvider {

	private int colindex;
	private int typePuce;
	NumberFormat numberFormat = NumberFormat.getNumberInstance(Locale.FRANCE);

	/** Constructeur privé */
	public CNVTableLabelProvider(int colindex, int typePuce) {
		this.colindex = colindex;
		this.typePuce = typePuce;
	}

	@Override
	public String getText(Object element) {
		String result = null;
		Alteration alt = (Alteration) element;
		String bandes = "";
		String resultatVerifBM = "";
		String resultatVerifFish = "";

		switch (colindex) {
		case 0:
			result = alt.getChromosome();
			break;
		case 1: {
			List<Bande> lbandes = ReferentielServiceImpl.getInstance().getBandesByCNV(alt);
			if (lbandes != null) {
				for (Bande bande : lbandes) {
					bandes += bande.getLibelle() + "-";
				}
			}
			if (bandes.length() > 2) {
				bandes = bandes.substring(0, bandes.length() - 1);
			}
			result = bandes;
			break;
		}
		case 2: {
			float taille = ((float) alt.getFinHg19() - (float) alt.getDebutHg19()) / 1000;

			result = "" + ((float) Math.round(taille * 100)) / 100;
			break;
		}
		case 3: {
			if (alt.getSondeDebut() == null || alt.getSondeDebut().isEmpty()) {
				alt.setSondeDebut(ReferentielServiceImpl.getInstance().getProbeByAlteration(alt.getChromosome(), alt.getDebutHg19(), typePuce));
			}
			if (alt.getSondeDebut() != null) {
				result = ("" + numberFormat.format(alt.getDebutHg19())).trim() + "(" + alt.getSondeDebut().trim() + ")";
			} else {
				result = ("" + numberFormat.format(alt.getDebutHg19())).trim();
			}
			break;
		}
		case 4: {
			if (alt.getSondeFin() == null || alt.getSondeFin().isEmpty()) {
				alt.setSondeFin(ReferentielServiceImpl.getInstance().getProbeByAlteration(alt.getChromosome(), alt.getFinHg19(), typePuce));
			}
			if (alt.getSondeFin() != null) {
				result = ("" + numberFormat.format(alt.getFinHg19())).trim() + "(" + alt.getSondeFin().trim() + ")";
			} else {
				result = ("" + numberFormat.format(alt.getFinHg19())).trim();
			}
			break;
		}
		case 5:
			result = "" + alt.getNbCopie();
			break;
		case 6:
			result = "" + alt.getLog2R();
			break;
		case 7:
			result = "" + alt.getNbMarqueurs();
			break;
		case 8:
			result = "" + ReferentielServiceImpl.getInstance().getNbGeneNCBIByBorne(alt, 0);
			break;
		case 9: {
			if (alt.getEstCnpCalc()) {
				result = "CNPc";
			} else {
				result = "-";
			}
			break;
		}
		case 10: {
			if (alt.isaVerifFish()) {
				List<VerifFish> verifFishs = ResultatServiceImpl.getInstance().getVerifFishByIdAlteration(alt.getIdAlteration());
				if (verifFishs != null && verifFishs.size() > 0) {
					for (VerifFish verifFish2 : verifFishs) {
						if (verifFish2.getDetecte() != null && verifFish2.getDetecte()) {
							resultatVerifFish += "CNV détecté par FISH. ";
							if (verifFish2.getHerite() == null) {
								resultatVerifFish += "Origine parentale non vérifiée.";
							} else {
								resultatVerifFish += verifFish2.getHerite() + ". ";
							}
						} else if (verifFish2.getDetecte() == null) {
							resultatVerifFish += "Demandée";
						} else if (!verifFish2.getDetecte()) {
							resultatVerifFish += "CNV non détecté par FISH. ";
						}
					}

				} else {
					resultatVerifFish += "Demandée";
				}
			} else {
				resultatVerifFish += "-";
			}
			result = resultatVerifFish;
			break;
		}
		case 11: {
			if (alt.isaVerifBioMol()) {
				List<VerifBiomol> verifBiomols = ResultatServiceImpl.getInstance().getVerifBiomolByIdAlteration(alt.getIdAlteration());
				if (verifBiomols != null && verifBiomols.size() > 0) {
					for (VerifBiomol verifBiomol2 : verifBiomols) {

						if (verifBiomol2.getDetecte() != null) {
							if (verifBiomol2.getDetecte() == 1) {
								resultatVerifBM += "CNV détecté par qPCR. ";
								if (verifBiomol2.getHerite() == null) {
									resultatVerifBM += "Origine parentale non vérifiée.";
								} else {
									resultatVerifBM += verifBiomol2.getHerite() + ". ";
								}
							} else if (verifBiomol2.getDetecte() == 0) {
								resultatVerifBM += "CNV non détecté par qPCR. ";
							} else if (verifBiomol2.getDetecte() == 2) {
								resultatVerifBM += "CNV ininterprétable en qPCR. ";
							}
						}
					}
				} else {

					resultatVerifBM += "Demandée";
				}
			} else {
				resultatVerifBM += "-";
			}
			result = resultatVerifBM;
			break;
		}
		case 12:
			if (alt.getManuel()) {
				result = "Man.";
			} else {
				result = "Auto.";
			}
			break;
		}

		return result;
	}

	public Color getBackground(Object element) {
		Alteration alt = (Alteration) element;
		Color color = null;
		if (alt.getNbCopie() == 0) {
			color = new Color(Display.getCurrent(), 255, 0, 0);
		} else if (alt.getNbCopie() == 1) {
			color = new Color(Display.getCurrent(), 255, 136, 245);
		} else if (alt.getNbCopie() == 2) {
			color = new Color(Display.getCurrent(), 255, 255, 255);
		} else if (alt.getNbCopie() == 3) {
			color = new Color(Display.getCurrent(), 103, 193, 244);
		} else if (alt.getNbCopie() == 4) {
			color = new Color(Display.getCurrent(), 0, 0, 255);
		} else {
			color = new Color(Display.getCurrent(), 96, 54, 138);
		}
		return color;
	}

}
